import { ObjectType } from 'typings/interfaces'
import { onMounted, ref, inject } from 'vue'

function useValidate() {
  const nodeRef = ref<ObjectType | null>(null)
  const addValidate = inject<any>('AddValidate')
  onMounted(() => {
    const validate = () =>
      new Promise((resolve, reject) => {
        nodeRef.value
          ?.validate()
          .then(() => resolve(true))
          .catch(() => reject(false))
      })
    addValidate(validate)
  })
  return {
    nodeRef,
  }
}

export default useValidate
